package com.yj.rselasticsearch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yj.common.constant.DataConstants;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.FileOptionLog;
import com.yj.common.core.domain.entity.SysRole;
import com.yj.common.core.domain.entity.SysUser;
import com.yj.common.core.domain.entity.TagInfo;
import com.yj.common.utils.FastUtils;
import com.yj.common.utils.StringUtils;
import com.yj.rselasticsearch.domain.dto.FileOptionLogDto;
import com.yj.rselasticsearch.mapper.FileOptionLogMapper;
import com.yj.rselasticsearch.service.FileInfoService;
import com.yj.rselasticsearch.service.FileOptionLogService;
import com.yj.system.service.ISysRoleService;
import com.yj.system.service.ISysUserService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class FileOptionLogServiceImpl extends ServiceImpl<FileOptionLogMapper,FileOptionLog> implements FileOptionLogService {

    @Resource
    private FileOptionLogMapper fileOptionLogMapper;

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysRoleService sysRoleService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return fileOptionLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FileOptionLog record) {
        return fileOptionLogMapper.insert(record);
    }

    @Override
    public int insertSelective(FileOptionLog record) {
        return fileOptionLogMapper.insertSelective(record);
    }

    @Override
    public FileOptionLog selectByPrimaryKey(Integer id) {
        return fileOptionLogMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FileOptionLog record) {
        return fileOptionLogMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FileOptionLog record) {
        return fileOptionLogMapper.updateByPrimaryKey(record);
    }

    @Override
    public AjaxResult getFileOptionLogByFileId(FileOptionLogDto fileOptionLogDto) {
        IPage<FileOptionLog> logIPage = new Page<>(fileOptionLogDto.getPageIndex(), fileOptionLogDto.getPageSize());
        logIPage = fileOptionLogMapper.selectPage(logIPage,new LambdaQueryWrapper<FileOptionLog>().eq(FileOptionLog::getFileInfoId,fileOptionLogDto.getId()).orderByDesc(FileOptionLog::getCreateTime));
        if (!FastUtils.checkNullOrEmpty(logIPage)){
            logIPage.getRecords().forEach(data->{
                SysUser sysUser = userService.selectUserByUserName(data.getCreateBy());
                List<SysRole> sysRoles =sysRoleService.selectRolesByUserIds(sysUser.getUserId());
                String roleName = "";
                for (SysRole sysRole : sysRoles) {
                    if (StringUtils.isEmpty(roleName)){
                        roleName =sysRole.getRoleName();
                    }else{
                        roleName += ","+sysRole.getRoleName();
                    }
                }
                if (data.getOptionType().equals(DataConstants.FILE_OPTION_TYPE.INSERT))
                {
                    String dateTime = data.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    String showContent  = String.format("操作人：%s(%s),操作时间：%s,新增文件。",roleName,sysUser.getNickName(),dateTime);
                    data.setShowContent(showContent);
                }else if (data.getOptionType().equals(DataConstants.FILE_OPTION_TYPE.UPDATE)){
                    String dateTime = data.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    String showContent  = String.format("操作人：%s(%s),操作时间：%s,变更：%s。",roleName,sysUser.getNickName(),dateTime,data.getOptionContent());
                    data.setShowContent(showContent);
                }
            });
        }
        return AjaxResult.success(logIPage);
    }

}
